﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class YaxNodeController : Controller
    {
        private YaxNodeInfo yaxinfo = new YaxNodeInfo(true);
        public ActionResult YaxNodeIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取设备组列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetYaxNodeList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string param = pvm.query;
                query = " and Nodename like '%" + param + "%'";
            }
            IList<YaxNodeInfo> yaxlist = yaxinfo.SelYaxNodeInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = yaxinfo._YaxNodecount;
            List<string> v;
            foreach (YaxNodeInfo item in yaxlist)
            {
                #region
                v = new List<string>();
                v.Add(item.NodeID.ToString());
                v.Add(item.Nodename);
                v.Add(item.Remark);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.NodeID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 删除设备组
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteYaxNode(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = yaxinfo.DeleteYaxNode(strId);
            return Json(result);
        }

        public ActionResult AddYaxNode()
        {
            return View();
        }

        /// <summary>
        /// 保存设备组（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveYaxNode(YaxNodeVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                YaxNodeInfo yax = new YaxNodeInfo()
                {
                    Nodename = model.Nodename,
                    Remark = model.Remark
                };
                result = yaxinfo.SaveYaxNode(yax);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditYaxNode(string id)
        {
            YaxNodeVM vm = new YaxNodeVM();
            YaxNodeInfo info = new YaxNodeInfo();
            yaxinfo = info.GetYaxNodeInfoOne(id);
            vm.NodeID = yaxinfo.NodeID;
            vm.Nodename = yaxinfo.Nodename;
            vm.Remark = yaxinfo.Remark;
            return View(vm);
        }

        /// <summary>
        /// 保存设备组（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveYaxNodeEdit(YaxNodeVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                YaxNodeInfo yax = new YaxNodeInfo()
                {
                    NodeID = model.NodeID,
                    Nodename = model.Nodename,
                    Remark = model.Remark
                };
                result = yaxinfo.SaveYaxNodeEdit(yax);
            }
            return Json(result);
        }
    }
}
